package com.ndphu.comic.gui.fragment.book;

import android.graphics.Bitmap;
import com.ndphu.comic.io.extend.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.ndphu.comic.R;
import com.ndphu.comic.io.Utils;

public class OnlineBookDescriptionFragment extends Fragment {
	ImageView cover;
	TextView desc;
	String bookCoverUrl;
	String bookDesc;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_online_book_description, container,
				false);
		cover = (ImageView) view.findViewById(R.id.fragment_book_details_cover);
		desc = (TextView) view.findViewById(R.id.fragment_book_details_desc);
		desc.setMovementMethod(new ScrollingMovementMethod());
		return view;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		new AsyncTask<Void, Void, Bitmap>() {

			@Override
			protected Bitmap doInBackground(Void... params) {
				if (bookCoverUrl == null)
					return null;
				try {
					return Utils.getBitmapFromUrl(bookCoverUrl, 480, 480);
				} catch (Exception ex) {
					ex.printStackTrace();
					return null;
				}
			}
			
			protected void onPostExecute(Bitmap result) {
				super.onPostExecute(result);
				if (result != null) {
					cover.setImageBitmap(result);
				}
			};
		}.execute();
		if (desc != null  && bookDesc != null) {
			desc.setText(Html.fromHtml(bookDesc));
		}
	}

	public void setBookCover(String coverUrl) {
		bookCoverUrl = coverUrl;
	}

	public void setBookDescription(String desc) {
		bookDesc = desc;
	}
}
